package com.lsh.oms.provider.service.order;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.lsh.base.common.json.JsonUtils;
import com.lsh.base.common.model.CommonResult;
import com.lsh.oms.api.model.order.returnro.ReturnHeadConfirmDto;
import com.lsh.oms.api.model.order.returnro.ReturnHeadDto;
import com.lsh.oms.api.service.order.IOrderReturnRestService;
import com.lsh.oms.core.constant.ReturnCode;
import com.lsh.oms.core.enums.OrderRoType;
import com.lsh.oms.core.enums.OrderStatus;
import com.lsh.oms.core.exception.BusinessException;
import com.lsh.oms.core.exception.ExceptionStatus;
import com.lsh.oms.core.model.order.OrderHead;
import com.lsh.oms.core.service.order.OrderQueryService;
import com.lsh.oms.core.service.order.OrderReturnService;
import com.lsh.oms.core.service.tool.ToolService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * Project Name: lsh-oms
 *
 * @author peter
 * @date 18/6/24
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Service(protocol = "rest", timeout = 30000)
@Path("order/return/pms")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
@Slf4j
@Deprecated
public class OrderReturnRestServiceImpl implements IOrderReturnRestService {

    @Autowired
    private OrderQueryService orderQueryService;

    @Autowired
    private OrderReturnService orderReturnService;

    @POST
    @Path("create")
    @Override
    public CommonResult<Object> create(ReturnHeadDto returnHeadDto) {
        String logSign = "logSign : " + ToolService.getLogSign();
        long startTimeMs = System.currentTimeMillis();

        CommonResult<Object> commonResult = new CommonResult<>();

        try {
            if (returnHeadDto == null) {
                throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "returnHeadDto 请求参数不合法，请检验。");
            }

            if (returnHeadDto.getOrderCode() == null) {
                throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "orderCode 请求参数不合法，请检验。");
            }

            if (returnHeadDto.getReturnOrderType() == null) {
                throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "returnOrderType 请求参数不合法，请检验。");
            }

            if (returnHeadDto.getReturnOrderType().equals(OrderRoType.AFTER_RECEIPT.getValue())) {
                if (StringUtils.isBlank(returnHeadDto.getAsfId())) {
                    throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "asfId 请求参数不合法，请检验。");
                }
            }

            log.info(logSign + ": pms返仓入参 －－ {}", JsonUtils.obj2Json(returnHeadDto));

            OrderHead orderHead = orderQueryService.findByCode(returnHeadDto.getOrderCode(), false, false);
            if (orderHead == null) {
                log.info(logSign + "请求耗时:" + (startTimeMs - System.currentTimeMillis()) + "ms");
                throw new BusinessException(ReturnCode.FIND_ORDER_IS_NULL, "用户订单号未查询到订单");
            }

            if (orderHead.getOrderStatus().compareTo(OrderStatus.ORDER_DELIVERY.getIndex()) < 0) {
                throw new BusinessException(ReturnCode.ORDER_STATUS_ERROR, "订单状态是" + OrderStatus.getName(orderHead.getOrderStatus()) + "，请检验。");
            }

            commonResult = orderReturnService.createRO(returnHeadDto, orderHead, logSign);

        } catch (BusinessException e) {
            commonResult.setCode(e.getCode());
            commonResult.setMessage(e.getMessage());
            log.error("业务异常code : " + e.getCode() + ",异常信息: " + e.getMessage(), e);
        } catch (Exception e) {
            commonResult.setCode(ExceptionStatus.E3001001.getCode());
            commonResult.setMessage(ExceptionStatus.E3001001.getMessage());
            log.error("业务异常code : " + ExceptionStatus.E3001001.getCode() + ",异常信息: " + e.getMessage(), e);
        }

        return commonResult;
    }


    @POST
    @Path("confirm")
    @Override
    public CommonResult<Object> confirm(ReturnHeadConfirmDto headConfirmDto) {
        String logSign = "logSign : " + ToolService.getLogSign();

        CommonResult<Object> commonResult = new CommonResult<>();

        try {
            if (headConfirmDto == null) {
                throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "returnHeadDto 请求参数不合法，请检验。");
            }

            if (headConfirmDto.getOrderCode() == null) {
                throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "orderCode 请求参数不合法，请检验。");
            }

//            if (headConfirmDto.getReturnOrderType() == null) {
//                throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "returnOrderType 请求参数不合法，请检验。");
//            }

            log.info(logSign + ": 多点 返仓入参 －－ {}", JsonUtils.obj2Json(headConfirmDto));

            OrderHead orderHead = orderQueryService.findByCode(headConfirmDto.getOrderCode(), false, false);
            if (orderHead == null) {

                throw new BusinessException(ReturnCode.FIND_ORDER_IS_NULL, "用户订单号未查询到订单");
            }

            if (orderHead.getOrderStatus().compareTo(OrderStatus.ORDER_DELIVERY.getIndex()) < 0) {
                throw new BusinessException(ReturnCode.ORDER_STATUS_ERROR, "订单状态是" + OrderStatus.getName(orderHead.getOrderStatus()) + "，请检验。");
            }

            commonResult = orderReturnService.confirmRO(headConfirmDto, logSign);

        } catch (BusinessException e) {
            commonResult.setCode(e.getCode());
            commonResult.setMessage(e.getMessage());
            log.error("业务异常code : " + e.getCode() + ",异常信息: " + e.getMessage(), e);
        } catch (Exception e) {
            commonResult.setCode(ExceptionStatus.E3001001.getCode());
            commonResult.setMessage(ExceptionStatus.E3001001.getMessage());
            log.error("业务异常code : " + ExceptionStatus.E3001001.getCode() + ",异常信息: " + e.getMessage(), e);
        }

        return commonResult;
    }


}
